package business_opportunity

import (
	"encoding/json"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/http_client"
	dto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/project"
	reportService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"

	controllerDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto"
	projectDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto/project"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	"git.myscrm.cn/golang/stark/v4"

	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	"github.com/goinggo/mapstructure"

	"github.com/gin-gonic/gin"
)

type ProjectController struct {
	controller.BaseController
	searchProject  project.SearchProjectServiceIface
	reportService  reportService.ReportServiceIface
	projectService project.ProjectServiceIface
}

// NewProjectController 项目
func NewProjectController(
	searchProject project.SearchProjectServiceIface,
	reportService reportService.ReportServiceIface,
	projectService project.ProjectServiceIface,
) *ProjectController {
	return &ProjectController{
		searchProject:  searchProject,
		reportService:  reportService,
		projectService: projectService,
	}
}

// RegisterRouter
// 注册路由
func (c *ProjectController) RegisterRouter(g *gin.RouterGroup) {

	/****  聚合项目  *****/
	// 搜索项目
	g.POST("/project/search-project", c.searchProjectList)
	// 搜索项目
	g.GET("/project/fuzzy-search-project", c.fuzzySearchProject)
	// 项目详情
	g.POST("/project/detail", c.projectDetail)
	// 项目详情 - 拿地列表
	g.POST("/project/detail-land", c.projectDetailLand)
	// 项目详情 - 在建列表
	g.POST("/project/detail-build", c.projectDetailBuild)
	// 项目详情 - 开盘列表
	g.POST("/project/detail-sale", c.projectDetailSale)
	// 项目详情 - 招标列表
	g.POST("/project/detail-bid", c.projectDetailBid)

	/****  拿地和开盘  *****/
	// 拿地详情
	g.POST("/project/taken-land-detail", c.takenLandDetail)
	// 开盘列表
	g.POST("/project/sale-list", c.saleList)
	// 开盘详情
	g.POST("/project/sale-detail", c.saleDetail)
	// 新房销售
	g.POST("/project/new-house", c.newHouse)
	// 获取项目主体
	g.GET("/project/get-top-project", c.getTopProject)
}

func (c *ProjectController) SetAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

// 找项目-土地储备-内容详情（土地详情）
func (c *ProjectController) takeLandDetail(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	path := "/pub/40220901/takeLandPub/detail"
	c.commonSearch(ctx, path)
}

func (c *ProjectController) commonSearch(ctx *gin.Context, path string) {
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.searchProject.CommonSearch(ctx, path, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	gfyxModelResponse := &http_client.GfyxModelResponse{}
	//将 map 转换为指定的结构体
	err = mapstructure.Decode(rs, gfyxModelResponse)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	if gfyxModelResponse.Error.Exception.Message != "" {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, gfyxModelResponse.Error.Exception.Message)
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) mohurdProject(ctx *gin.Context) {
	mohurdProjectListRequest := &projectDto.MohurdProjectListRequest{}
	if err := ctx.ShouldBind(mohurdProjectListRequest); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if mohurdProjectListRequest.Page == 0 {
		mohurdProjectListRequest.Page = constants.FirstPage
	}
	if mohurdProjectListRequest.PageSize == 0 {
		mohurdProjectListRequest.PageSize = constants.PageSize
	}

	rs, err := c.searchProject.MohurdProjectList(ctx, mohurdProjectListRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) ProjectDetail(ctx *gin.Context) {
	request := &controllerDto.CommonIdRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:参数id不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数id不能为空")
		return
	}
	rs, err := c.searchProject.ProjectDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) AboutCompany(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.AboutCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) BiddingList(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.BiddingList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) Contract(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.Contract(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) Construction(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.Construction(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) License(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.License(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) CompletionRecord(ctx *gin.Context) {
	request := &projectDto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	if request.Id == "" {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:id参数不能为空")
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id参数不能为空")
		return
	}

	rs, err := c.searchProject.CompletionRecord(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *ProjectController) setAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *ProjectController) searchProjectList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.SearchProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SearchProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/search-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) fuzzySearchProject(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.FuzzySearchProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.FuzzySearchProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/fuzzy-search-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailLand(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.ProjectDetailLandRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailLand(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-land err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailBuild(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.ProjectDetailBuildRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailBuild(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-build err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailSale(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.ProjectDetailSaleRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailSale(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-sale err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailBid(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.ProjectDetailBidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailBid(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-bid err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) takenLandDetail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.TakenLandDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.TakenLandDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/taken-land-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) saleList(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.SaleListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SaleList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/sale-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) saleDetail(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.SaleDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SaleDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/sale-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) newHouse(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.NewHouseRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.NewHouse(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/new-house err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) getTopProject(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	req := &dto.GetTopProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.GetTopProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/get-top-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}
